const Model = require('../models/hanziRadicalModel')

const getAllHanziRadicals = async (ctx) => {
  ctx.body = { success: true, data: await Model.getAllHanziRadicals() }
}

const getHanziRadicalById = async (ctx) => {
  const { id } = ctx.params
  const hanziRadical = await Model.getHanziRadicalById(id)
  if (!hanziRadical) ctx.throw(404, 'Hanzi Radical not found')
  ctx.body = hanziRadical
}

const createHanziRadical = async (ctx) => {
  const radicalData = ctx.request.body
  const radicalId = await Model.createHanziRadical(radicalData)
  ctx.status = 201
  ctx.body = { id: radicalId, ...radicalData }
}

const updateHanziRadical = async (ctx) => {
  const { id } = ctx.params
  const radicalData = ctx.request.body
  await Model.updateHanziRadical(id, radicalData)
  ctx.body = { success: true }
}

const deleteHanziRadical = async (ctx) => {
  const { id } = ctx.params
  await Model.deleteHanziRadical(id)
  ctx.status = 204
}

module.exports = {
  getAllHanziRadicals,
  getHanziRadicalById,
  createHanziRadical,
  updateHanziRadical,
  deleteHanziRadical,
}